package com.sfzd5.exam.helplibrary.exam;

import com.sfzd5.exam.helplibrary.exam.answersheet.AnswerSheetQuestion;
import com.sfzd5.exam.helplibrary.exam.answersheet.Area;
import com.sfzd5.exam.helplibrary.exam.answersheet.BlankAnswerSheetQuestion;
import com.sfzd5.exam.helplibrary.exam.answersheet.BlankInfo;
import com.sfzd5.exam.helplibrary.exam.answersheet.BlankLine;
import com.sfzd5.exam.helplibrary.exam.answersheet.ExpoundAnswerSheetQuestion;
import com.sfzd5.exam.helplibrary.exam.answersheet.ExpoundChildQuestion;

import java.util.ArrayList;
import java.util.List;

/**
 * 输出内容区域 15 20 180 257
 */
public class MakePage {
    public static List<Integer> getBlankLines(int qid, List<BlankAnswerSheetQuestion> blankList) {
        // 行宽
        double pw = PageA4.contextWidthm() - 2 * PageA4.areaMarginm; //176
        int lineWidth = 99;

        List<Integer> questionLines = new ArrayList<>();

        StringBuilder sb = new StringBuilder();
        boolean isQuestionFirstBlank;

        //逐题逐空逐标题判断是否超行，题号与首空为一整体
        for (BlankAnswerSheetQuestion blankQuestion : blankList) {
            List<BlankLine> lines = new ArrayList<>();
            //每题都要新开一行，所以清空
            sb.delete(0, sb.length());//清空

            //新小题的第一行
            BlankLine blankLine = new BlankLine();
            if (qid < 10) sb.append(" ");
            sb.append(qid).append(".");
            blankLine.hasQid = true;

            for (BlankInfo blankInfo : blankQuestion.getBlankInfoList()) {
                //计算还可以添加多少个空格
                int canAdd_Count = lineWidth - sb.length();
                if(canAdd_Count>blankInfo.width) {
                    // 可以将本空全部添加到本行
                    for (int bi = 0; bi < blankInfo.width; bi++)
                        sb.append("_");
                    //添加一个空格隔开空
                    sb.append(" ");
                } else {
                    //不可以将本空全部添加到本行，计算剩余的_字符数
                    int sykg = blankInfo.width;
                    // 最少也得能打下两个空格
                    if(canAdd_Count>2){
                        sykg -= canAdd_Count;
                        for (int bi = 0; bi < canAdd_Count; bi++)
                            sb.append("_");
                    }

                    //将剩余空格添加进去
                    while (sykg>0) {
                        //将前面添加的内容添加到列表里
                        blankLine.str = sb.toString();
                        lines.add(blankLine);

                        //创建一个新行
                        blankLine = new BlankLine();
                        sb.delete(0, sb.length());//清空

                        //取小值，也就是最多可以添加的字符数，并添加
                        int ccc = lineWidth>sykg ? sykg : canAdd_Count;
                        for (int bi = 0; bi < ccc; bi++)
                            sb.append("_");

                        if(lineWidth>=sykg){
                            // 可以添加完，则返回完成，最后加一个空格隔开空
                            sb.append(" ");
                            sykg = 0;
                        } else {
                            //没添加完，计算剩余空格数，继续添加
                            sykg = sykg - lineWidth;
                        }
                    }
                }
            }
            blankLine.str = sb.toString();
            lines.add(blankLine);
            qid++;
            questionLines.add(lines.size());
        }
        return questionLines;
    }

    public int getChoiceLineCount(Area choiceArea){
        if(choiceArea.areaType != Area.AreaType.Choice)
            return -1;
        int lineCount = (int)Math.ceil(choiceArea.answerSheetQuestionList.size()/4d);
        choiceArea.lineCount = lineCount;
        return lineCount;
    }

    public int getExpoundLineCount(Area expoundArea){
        if(expoundArea.areaType != Area.AreaType.Expound)
            return -1;
        ArrayList<ExpoundAnswerSheetQuestion> expoundList = new ArrayList<>();
        for (AnswerSheetQuestion answerSheetQuestion : expoundArea.answerSheetQuestionList) {
            expoundList.add(new ExpoundAnswerSheetQuestion(answerSheetQuestion));
        }
        int lineCount = 0;
        for(ExpoundAnswerSheetQuestion eq: expoundList){
            for(ExpoundChildQuestion ecq : eq.getChildQuestionList()){
                lineCount += ecq.lineNum;
            }
        }
        expoundArea.lineCount = lineCount;
        return lineCount;
    }

    public int getBlankLineCount(Area blankArea){
        //检测类型
        if(blankArea.areaType != Area.AreaType.Blank)
            return -1;

        ArrayList<BlankAnswerSheetQuestion> blankList = new ArrayList<>();
        for (AnswerSheetQuestion answerSheetQuestion : blankArea.answerSheetQuestionList) {
            blankList.add(new BlankAnswerSheetQuestion(answerSheetQuestion));
        }
        List<Integer> blankLines = getBlankLines(10, blankList);
        for(Integer c : blankLines)
            blankArea.lineCount += c;
        return blankArea.lineCount;
    }

    public double getAreaHeight(Area area, int line){
        int lineCount = 0;
        //区域高度计算公式 = 行高 * （行数+标题一行） + 边距*2
        if (area.areaType == Area.AreaType.Choice) {
            lineCount = getChoiceLineCount(area);
        } else if (area.areaType == Area.AreaType.Blank) {
            lineCount = getBlankLineCount(area);
        } else if (area.areaType == Area.AreaType.Expound) {
            lineCount = getExpoundLineCount(area);
        }
        if(line == 0) {
            return PageA4.lineHightm + PageA4.lineHightm * lineCount + PageA4.areaMarginm * 2;
        } else {
            return PageA4.lineHightm * (lineCount - line) + PageA4.areaMarginm * 2;
        }
    }

}
